<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>高校宿舍管理系统-报修列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="#"/>
    <link rel="stylesheet" href="../layui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../layui/css/public.css" media="all">
    <style type="text/css">
        #dataFrm .layui-form-item {
            margin-top: 30px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">宿舍区</label>
                            <div class="layui-input-inline">
                                <input type="text" name="brarea" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">楼栋</label>
                            <div class="layui-input-inline">
                                <input type="text" name="brbid" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <select name="handleStatu" id="handleStatu" class="layui-input">
                                    <option value="0" selected>-请选择-</option>
                                    <option value="1">已处理</option>
                                    <option value="2">未处理</option>
                                    <option value="3">正在处理</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i>查 询
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-edit-btn" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>批量更改
                </button>
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="export"><i
                        class="layui-icon layui-icon-export"></i>导出
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="detail">
                <i class="layui-icon layui-icon-about"></i>详情
            </a>
        </script>

        <div style="display: none;padding: 5px" id="batchEditStatusWindow">
            <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">维修状态</label>
                    <div class="layui-input-block">
                        <select name="batchEditStatus" id="batchEditStatus" class="layui-input">
                            <option value="1" selected>已处理</option>
                            <option value="2">未处理</option>
                            <option value="3">正在处理</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit">
                            <span class="layui-icon layui-icon-add-1"></span>提交
                        </button>
                    </div>
                </div>
            </form>
        </div>

    </div>
</div>
<script src="../layui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>

    // 保存行数据，用来渲染iframe的input
    var detailData;

    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '/dm/repair/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 80, title: 'ID', align: "center"},
                    {field: 'subtime', width: 130, title: '提交日期', align: "center"},
                    {field: 'stuname', width: 120, title: '学生姓名', align: "center"},
                    {field: 'stutel', width: 150, title: '联系方式', align: "center"},
                    {field: 'brarea', title: '宿舍区', width: 100, align: "center"},
                    {field: 'brbid', width: 80, title: '楼栋', align: "center"},
                    {field: 'brrid', width: 100, title: '寝室', align: "center"},
                    {field: 'type', width: 120, title: '报修类型', align: "center"},
                    {field: 'content', minWidth: 80, title: '描述'},
                    {field: 'status', width: 100, title: '维修状态', align: "center"},
                    {title: '操作', width: 100, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [5, 10, 20, 25, 50, 100],
            limit: 5,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            console.log(data);
            var obj = {};
            switch (data.field.handleStatu) {
                case "0":
                    obj['status'] = "";
                    break;
                case "1":
                    obj['status'] = "已处理";
                    break;
                case "2":
                    obj['status'] = "未处理";
                    break;
                case "3":
                    obj['status'] = "正在处理";
                    break;
            }
            if (data.field.brarea != null && data.field.brarea != "") {
                obj['brarea'] = data.field.brarea;
            }
            if (data.field.brbid != null && data.field.brbid != "") {
                obj['brbid'] = data.field.brbid;
            }
            data.field = obj;

            //执行搜索重载
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        /**
         * toolbar表格头部工具栏监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'export') {
                var status = $("#handleStatu").val();
                if (status != 0) {
                    window.location.href = "/dm/repair/export.action?status=" + status;
                } else {
                    window.location.href = "/dm/repair/export.action";
                }
            } else if (obj.event === 'edit') {
                // 打开状态更改窗口
                openEditStatusWindow();
            }
        });

        var url;//更改状态的url
        var windowIndex;//更改窗口的索引

        // 维修状态更改窗口
        function openEditStatusWindow() {
            windowIndex = layer.open({
                type: 1,
                title: "更改维修状态",
                area: ["350px", "250px"],
                content: $("#batchEditStatusWindow"),
                success: function () {
                    $("#dataFrm")[0].reset();
                    url = "/dm/repair/edit";
                }
            });
        }

        form.on("submit(doSubmit)", function (data) {
            // 获取选中行的id，用来做后端的查询条件
            var checkStatus = table.checkStatus('currentTableId');
            var checkData = checkStatus.data;
            var ids = [];
            var queryParams = [];
            for (var i = 0; i < checkData.length; i++) {
                ids.push(checkData[i].id);
            }
            // 获取更改后的状态
            var status = data.field.batchEditStatus;
            console.log(status);
            queryParams.push({"ids": ids, "status": status});
            var params = JSON.stringify(queryParams);
            $.post(url,{params:params},function(result){
                if(result.success){
                    //刷新数据表格
                    tableIns.reload();
                    //关闭窗口
                    layer.close(windowIndex);
                }
                //提示信息
                layer.msg(result.msg);
            },"json");
            // 禁止页面刷新
            return false;
        });

        // 行工具栏监听
        table.on('tool(currentTableFilter)', function (obj) {
            console.log(obj);
            if (obj.event === 'detail') {
                detailData = JSON.stringify(obj.data);
                var index = layer.open({
                    title: '报修详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: false,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/dm/repair/detail.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });
    });
</script>

</body>
</html>